package org.xm.sk.controller.dbase;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.xm.sk.exception.DataNotFoundException;
import org.xm.sk.service.data.BaseService;
import org.xm.sk.vo.ob.Ids;
import org.xm.sk.vo.ob.MapList;
import org.xm.sk.vo.ob.PageConf;
import org.xm.sk.vo.ob.ResultMsg;
import org.xm.sk.vo.tb.Item;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@SuppressWarnings("unchecked")
@Api(tags = { "item" })
@RestController
@RequestMapping(value="/items")
public class ItemController {
    
    @Autowired
    private MessageSource messages;
    @Autowired
    private BaseService<Item> itemService;
    
    @ApiOperation(value="创建项目", notes="根据item对象创建项目，使用此接口需要ADMIN权限")
    @ApiImplicitParam(name = "item", value = "项目详细实体item", required = true, dataType = "Item")
    @PreAuthorize("hasRole('ADMIN')")
    @RequestMapping(value="", method=RequestMethod.POST)
    public ResultMsg add(@RequestBody Item item) {
        Item u = null;
        if (null != item.getId() && 0 < item.getId())
            u = (Item) itemService.findById(item.getId());
        else
            u = item;
        u.setName(item.getName());
        if (null != u.getId() && 0 < u.getId())
            itemService.update(u);
        else
            itemService.add(u);
        return new ResultMsg("200", messages.getMessage("success", null, "SUCCESS", null), new Date());
    }

    @ApiIgnore()
    @ApiOperation(value="删除项目", notes="根据URI的id来指定删除对象")
    @ApiImplicitParam(name = "id", value = "项目ID", required = true, paramType = "path", dataType = "int")
    @RequestMapping(value="/{id}", method=RequestMethod.DELETE)
    public ResultMsg delete(@PathVariable Integer id) {
        itemService.delete(id);
        return new ResultMsg("200", messages.getMessage("success", null, "SUCCESS", null), new Date());
    }
    
    @ApiOperation(value="删除项目", notes="根据POST的id列表来指定删除对象，可批量")
    @ApiImplicitParam(name = "ids", value = "项目ID集", required = true, dataType = "Ids")
    @RequestMapping(value="/delete", method=RequestMethod.DELETE)
    public ResultMsg Batchdelete(@RequestBody Ids ids) {
        itemService.delete(ids.getIds());
        return new ResultMsg("200", messages.getMessage("success", null, "SUCCESS", null), new Date());
    }
    
    @ApiIgnore()
    @ApiOperation(value="更新项目详细信息", notes="根据URI的id来指定更新对象，并根据传过来的item信息来更新项目详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "项目ID", required = true, paramType = "path", dataType = "int"),
            @ApiImplicitParam(name = "item", value = "待更新的项目详细实体item", required = true, dataType = "Item")
    })
    @RequestMapping(value="/{id}", method=RequestMethod.PUT)
    public ResultMsg update(@PathVariable Integer id, @RequestBody Item item) throws DataNotFoundException {
        Item u = (Item) itemService.findById(id);
        if (null == u) {
            throw new DataNotFoundException(Item.class.getName());
        }
        item.setId(id);
        itemService.update(item);
        return new ResultMsg("200", messages.getMessage("success", null, "SUCCESS", null), new Date());
    }

    @ApiIgnore()
    @ApiOperation(value="获取项目详细信息", notes="根据URI的id来获取项目详细信息")
    @ApiImplicitParam(name = "id", value = "项目ID", required = true, paramType = "path", dataType = "int")
    @RequestMapping(value="/{id}", method=RequestMethod.GET)
    public Item load(@PathVariable Integer id) throws DataNotFoundException {
        Item u = (Item) itemService.findById(id);
        if (null == u) {
            throw new DataNotFoundException(Item.class.getName());
        }
        return u;
    }
    
    @ApiOperation(value="获取项目列表", notes="根据提交的分页信息列出所以项目信息")
    @ApiImplicitParam(name = "pageConf", value = "分页信息实体", required = true, dataType = "PageConf")
    @RequestMapping(value={"/list"}, method=RequestMethod.POST)
    public MapList<Item> list(@RequestBody PageConf pageConf) {
        List<Item> u = null;
        Long allCount = null;
        Object o = itemService.findAllByPage(pageConf);
        if (o instanceof Page) {
            allCount = ((Page<Item>) o).getTotalElements();
            u = ((Page<Item>) o).getContent();
        } else {
            allCount = (long) ((List<Item>) o).size();
            u = (List<Item>) o;
        }
        return new MapList<Item>(allCount, u);
    }

    // --------------------- 业务具体接口 ---------------------

}
